#ifndef _LIB_KERNEL_BITMAP_H_
#define _LIB_KERNEL_BITMAP_H_

#include "global.h"
#include "stdint.h"

#define BITMAP_MASK 1

struct bitmap
{
  /**
   * @brief byte length of bits
   * @details value multiple 8 is the amount of resources managed
   */
  uint32_t bitmap_bytes_lens;
  /**
   * @brief point to the address that specifies the use of resources
   * @details allocate memory according to bitmap_bytes_lens during
   * initialization
   */
  uint8_t* bits;
};

/**
 * @brief 从 bitmap 数组地址开始，以数组长度进行清零
 *
 * @param map bitmap 数组地址
 */
void bitmap_init(struct bitmap* map);

/**
 *@brief 已知位图位索引，在数组中找到这个位，判断是1还是0
 *
 * @param map bitmap 数组地址
 * @param bit_index 取第几个 bit
 *
 * @return 为 1 返回 true；否则返回 false
 */
bool bitmap_scan_test(struct bitmap* map, uint32_t bit_index);

/**
 * @brief 位图中连续申请 cnt 个位
 *
 * @param map bitmap 数组地址
 * @param cnt 申请位的个数
 *
 * @return 成功返回 bit_index,失败返回 -1
 */
int bitmap_scan(struct bitmap* map, uint32_t cnt);

/**
 * @brief Set the value of a bit at the specified index in the bitmap.
 *
 * This function sets the value of the bit at the specified index in the bitmap.
 *
 * @param map Pointer to the bitmap structure.
 * @param bit_index Index of the bit to be set.
 * @param value Value to be set (0 or 1).
 */
void bitmap_set(struct bitmap* map, uint32_t bit_index, int8_t value);

#endif
